﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Sita.Data.Context;
using Sita.Entities;
using Sita.Data.Core;

namespace Sita.Data.Repositories
{
    public class VehicleRepository : GenericRepository<Vehicle>
    {
        public VehicleRepository() :
            base(new SitaContext())
        { }

        public IList<Vehicle> GetPaginatedByBusinessUnitID(int businessUnitID, int pageIndex, int pageSize, string sortColumn, bool ascending, int customerID, out int totalRows)
        {
            RepositoryHelper<Vehicle> Helper = new RepositoryHelper<Vehicle>();
            var OrderByExpr = Helper.GetOrderExpression(sortColumn);

            IQueryable<Vehicle> ReturnSet;


            ReturnSet = (base.StoreContext as ISitaContext).Vehicles
                .Where(r => r.BusinessUnitID == businessUnitID && (customerID == 0 || r.CustomerID == customerID))
                .Select(buu => buu);

            totalRows = ReturnSet.Count();

            return (ascending)
                                ?
                                    ((IOrderedQueryable<Vehicle>)System.Linq.Queryable.OrderBy(ReturnSet, OrderByExpr))
                                        .Skip(pageIndex * pageSize)
                                        .Take(pageSize)
                                        .Include("VehicleType")
                                        .Include("VehicleBrand")
                                        .Include("Customer")
                                        .ToList()
                                :
                                    ((IOrderedQueryable<Vehicle>)System.Linq.Queryable.OrderByDescending(ReturnSet, OrderByExpr))
                                        .Skip(pageIndex * pageSize)
                                        .Take(pageSize)
                                        .Include("VehicleType")
                                        .Include("VehicleBrand")
                                        .Include("Customer")
                                        .ToList();

        }

        public Vehicle GetByIDWithWO(int VehicleID)
        {
            Vehicle ReturnSet;
            ReturnSet = (base.StoreContext as ISitaContext).Vehicles
                .Where(r => r.VehicleID == VehicleID)
                .Include("WorkOrders")
                .SingleOrDefault();

            return ReturnSet;
        }
    }
}
